﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BrokerBase.Lib.Product;

namespace BrokerBase.Lib.Policy
{
    public class PolicyActing:IPolicyActing
    {
        private ISchemeActing sa;
        private IPolicy policy;
        private IPolicyPeriodActing[] ppas;
        private IDictionary<IAuxProperty, object> auxConfigs;

        public PolicyActing(ISchemeActing schemeActing,IPolicy policy)
        {
            sa = schemeActing;
            this.policy = policy;

            ppas = new IPolicyPeriodActing[schemeActing.MaxPeriod];


        }

        private void InitAuxConfigs()
        {
            IList<IAuxProperty> auxs = policy.Product.AuxProperties;

            foreach (IAuxProperty aux in auxs)
            {
                if (aux.IsCollection)
                    AuxConfigs[aux] = aux.ListItems[0];//默认集合的第一个项目
                else
                    AuxConfigs[aux] = aux.GetValue();
            }
        }

        public ISchemeActing SchemeActing { get { return sa; } }

        public IPolicy Policy { get { return policy; } }

        public IPolicyPeriodActing[] PeriodActings { get { return ppas;} }

        public double CompensationTotal
        {
            get
            {
                throw new NotImplementedException();
            }
        }

        public IPolicyPeriodActing FinishPeriodActing 
        {
            get
            {
                IPolicyPeriodActing ppa = ppas.FirstOrDefault(p => p.IsCurrentFinish);

                if (ppa == null)
                {
                    ppa = PeriodActings[PeriodActings.Count() - 1];
                }

                return ppa;
            }
        }


        public IPolicyPeriodActing PaymentFinishActing { get; set; }


        public IDictionary<IAuxProperty,object> AuxConfigs 
        {
            get
            {
                if (auxConfigs == null)
                {
                    auxConfigs = new Dictionary<IAuxProperty, object>();

                    foreach (IAuxProperty aux in policy.Product.AuxProperties)
                    {
                        if (aux.IsCollection)
                            auxConfigs[aux] = aux.ListItems[0];//默认集合的第一个项目
                        else
                            auxConfigs[aux] = aux.GetValue();
                    }
                }

                return auxConfigs;
            }
        }

    }
}
